import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
// import Personalcenter from '../views/modules/Personal_center'
import {verificationToken} from "../apis/api"


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import("../views/Home.vue"),
    children: [
      //后台首页
      {
        path: '/',
        name: 'homepage',
        component: ()=>import("../views/modules/Home_page")
      },
      //账号列表
      {
        path: 'accountlist',
        name: 'accountlist',
        component: ()=>import("../views/modules/Account_list")
      },
      //添加账号
      {
        path: 'addaccount',
        name: 'addaccount',
        component: ()=>import("../views/modules/Add_account")
      },
      //修改密码
      {
        path: 'changepassword',
        name: 'changepassword',
        component: ()=>import("../views/modules/Change_password")
      },
      //店铺管理
      {
        path: 'storemanagemen',
        name: 'storemanagemen',
        component: ()=>import("../views/modules/Store_management")
      },
      //订单管理
      {
        path: 'ordermanagement',
        name: 'ordermanagement',
        component: ()=>import("../views/modules/Order_management")
      },
      //商品列表
      {
        path: 'commoditylist',
        name: 'commoditylist',
        component: ()=>import("../views/modules/Commodity_list")
      },
      //商品添加
      {
        path: 'addcommodity',
        name: 'addcommodity',
        component: ()=>import("../views/modules/Add_commodity")
      },
      //商品分类
      {
        path: 'commodityclassification',
        name: 'commodityclassification',
        component: ()=>import("../views/modules/Commodity_classification")
      },
      //商品统计
      {
        path: 'commoditystatistics',
        name: 'commoditystatistics',
        component: ()=>import("../views/modules/Commodity_statistics")
      },
      //订单统计
      {
        path: 'orderstatistics',
        name: 'orderstatistics',
        component: ()=>import("../views/modules/Order_statistics")
      },
      //个人中心
      {
        path: 'personalcenter',
        name: 'personalcenter',
        component: ()=>import("../views/modules/Personal_center")
      },
    ]
  },

   //404不存在的页面
   {
    path: '*',
    name: '404',
    component: ()=>import("../views/modules/Not_found")
  },
]

const router = new VueRouter({
  routes
})


//拦截地址栏跳转
router.beforeEach((to, from, next) => {
  // console.log(to, from, next())
  if (to.path == "/") {
    next()
  } else {
    verificationToken({
      token:localStorage.token
    })
      .then(res => {
        // console.log(res.data)
        if (res.data.code === 0) {
          next()
        } else {
          next("/")
        }
    })
  }
})


export default router
